const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
import { getUserByToken } from '../request/api'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login/login.vue')
  },

]
// 全路由配置
const allRoutes = {
  '/addattribute':{
    path:'/addattribute',
    component:()=>import(/* webpackChunkName: "good" */ '../views/attribute/addattribute.vue')
  },
  '/addtopic':{
    path:'/addtopic',
    component:()=>import(/* webpackChunkName: "good" */ '../views/topic/addtopic.vue')
  },
  '/edittopic':{
    path:'/edittopic/:id',
    component:()=>import(/* webpackChunkName: "good" */ '../views/topic/addtopic.vue')
  },
  '/editattribute':{
    path:'/editattribute/:id',
    component:()=>import(/* webpackChunkName: "good" */ '../views/attribute/addattribute.vue')
  },
  '/editcategory':{
    path:'/editcategory/:id',
    component:()=>import(/* webpackChunkName: "good" */ '../views/category/addcategory.vue')
  },
  '/editgood':{
    path:'/editgood/:id',
    component:()=>import(/* webpackChunkName: "good" */ '../components/goods/addgoods.vue')
  },
  '/editbrand':{
    path:'/editbrand/:id',
    component:()=>import(/* webpackChunkName: "good" */ '../views/brand/addbrand.vue')
  },
  '/addbrand':{
    path:'/addbrand',
    component:()=>import(/* webpackChunkName: "good" */ '../views/brand/addbrand.vue')
  },
  '/addcategory':{
    path:'/addcategory',
    component:()=>import(/* webpackChunkName: "good" */ '../views/category/addcategory.vue')
  },
  '/addgood':{
    path:'/addgood',
    component:()=>import(/* webpackChunkName: "good" */ '../components/goods/addgoods.vue')
  },
  '/good': {
    path: '/good',
    component: () => import(/* webpackChunkName: "good" */ '../views/good/Good.vue'),
  },
  '/category': {
    path: '/category',
    component: () => import(/* webpackChunkName: "category" */ '../views/category/Category.vue')
  },
  '/brand': {
    path: '/brand',
    component: () => import(/* webpackChunkName: "brand" */ '../views/brand/Brand.vue')
  },
  '/attribute': {
    path: '/attribute',
    component: () => import(/* webpackChunkName: "attribute" */ '../views/attribute/Attribute.vue')
  },
  '/order': {
    path: '/order',
    component: () => import(/* webpackChunkName: "order" */ '../views/order/Order.vue')
  },
  '/topic': {
    path: '/topic',
    component: () => import(/* webpackChunkName: "topic" */ '../views/topic/Topic.vue')
  },
  // '/editgood': {
  //   path: '/editgood:id',
  //   component: () => import(/* webpackChunkName: "topic" */ '../views/topic/Topic.vue')
  // }
}
const genRoutesFn = () => {
  const genRoutes = []
  let url = '';
  for (var i = 0; i < store.state.login.per.length; i++) {
    if (!url) {
      url = store.state.login.per[0].path
    }
    if (allRoutes[store.state.login.per[i].path]) {
      genRoutes.push(allRoutes[store.state.login.per[i].path])
    }
  }
  // addRoutes动态添加路由
  router.addRoutes([
    {
      path: '/',
      component: () => import(/* webpackChunkName: "homepage" */ '../views/HomePage/homePage.vue'),
      children: genRoutes
    }
  ])
  return url
}

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  
  if (localStorage.getItem('token')) {

    // 从登录页面跳转到/homepage
    if (from.path === '/login' && to.path === '/homepage') {
      // 动态生成当前登录用户的路由配置
      const url = genRoutesFn();
      return next(url);
    }
  
    else if ( store.state.login.per.length == 0) {

      // 发送请求
      getUserByToken().then(res => {
        if (res.errno === 0) {
          // 把用户权限存到vuex
          store.commit('changePer', res.data.menu)
          console.log(store.state.login.per)
          genRoutesFn();
          return next();
        }
      })
      // 已经登录，跳转到登录页面
    }
     else if (to.path == '/login') {
      return next(from)
    }
    next()
    // token不存在并且跳转到非登录界面
  } else if (to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})

export default router
